//
//  Cell.m
//  HYHoney
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Cell.h"
#import "Model.h"
#import "UIImageView+WebCache.h"
//#import "HeadScrollView.h"
#import "PushTopWebview.h"

@interface Cell ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIActivityIndicatorView *activity;
@property (nonatomic, retain)NSTimer *timer;

@end


@implementation Cell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createScroll];
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc{
    
    [_scroll release];
    [_activity release];
    [_array release];
    [_activity release];
    [_timer release];

    [super dealloc];
}


- (void)createScroll{

    self.scroll = [[UIScrollView alloc] init];
    
    self.scroll.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.scroll];
    
    // 添加 图片加载动画
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
   // [self.scroll addSubview:self.activity];
   // self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.userInteractionEnabled = YES;
    
    [self.scroll release];
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.scroll.frame = self.contentView.frame;
    [self.activity startAnimating];
    
    self.scroll.contentSize = CGSizeMake(self.contentView.frame.size.width * self.array.count, 0);
    self.scroll.contentOffset = CGPointMake(self.contentView.frame.size.width, 0);
        
    self.activity.center = self.scroll.center;
    
//    // model 传值
//    for (int i = 0; i < self.array.count; i++) {
//        
////        UIImageView *scrollImageView = [[UIImageView alloc] init];
//        
//        HeadScrollView *scrollImageView = [[HeadScrollView alloc] init];
//       
//        scrollImageView.userInteractionEnabled = YES;
//        
//        UILabel *labelOfTitle = [[UILabel alloc] init];
//        
//        scrollImageView.tag = i + 100;
//        
//        Model *model = self.array[i];
//        
//        [scrollImageView sd_setImageWithURL:[NSURL URLWithString:model.imglink] placeholderImage:[UIImage imageNamed:@"jzz"]];
//        
//        
//        scrollImageView.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - 1);
//        
//        labelOfTitle.frame = CGRectMake(self.frame.size.width * i + 15, self.frame.size.height - 35 , self.frame.size.width, 30);
//        labelOfTitle.textColor = [UIColor whiteColor];
//        
//        labelOfTitle.text = model.title;
//        
//  
//        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelScroll:)];
//        
//        [scrollImageView addGestureRecognizer:tap];
//        
//        [self.scroll addSubview:scrollImageView];
//        [self.scroll addSubview:labelOfTitle];
//        
//        [labelOfTitle release];
//    }
//
//    [self addTimer];
    [self.activity stopAnimating];
}


- (void)handelScroll:(UITapGestureRecognizer *)tap{

    NSLog(@"---dddddd---");
    
    NSInteger i = [tap view].tag - 100;
    NSLog(@"%ld", i);
    Model *model = self.array[i];
    
    /** 通知中心传值 */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passUrl" object:nil userInfo:@{@"content" : model}];
}



//// 开始拖拽的时候调用
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    
//    //    关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
//    //    [self.timer invalidate];
//    [self removeTimer];
//}
//
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    //    开启定时器
//    [self addTimer];
//}
//
//
//- (void)removeTimer{
//    
//    [self.timer invalidate];
//}


// 自动轮播
- (void)addTimer{
    
    // 判断计时器的运作
    if (!self.timer) {
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode: NSRunLoopCommonModes];
    }
    
}


- (void)timerAction{

//    CGFloat x = self.scroll.contentOffset.x;
//    CGFloat width = self.scroll.frame.size.width;
//    
//    if (x + width >= self.scroll.contentSize.width) {
//        
//        [self.scroll setContentOffset:CGPointMake(width, 0) animated:NO];
//    }else{
//    
//        [self.scroll setContentOffset:CGPointMake(x + width, 0) animated:YES];
//    }
    
    
    [UIView animateWithDuration:0.5 animations:^{
        self.scroll.contentOffset = CGPointMake(self.scroll.contentOffset.x + self.frame.size.width, 0);
        
    } completion:^(BOOL finished) {
        
        [self scrollViewDidEndDecelerating:self.scroll];
        
        finished = YES;
    }];
    
}




- (void)passModel:(NSMutableArray *)arr{

    self.array = arr;
    //NSLog(@"!!!");
    if (self.array.count) {
//        NSLog(@"!!!");
        [self.array addObject:self.array.firstObject];
        [self.array insertObject:[self.array objectAtIndex:self.array.count - 2] atIndex:0];
//        NSLog(@"!!!");
    }
   
    // model 传值
    for (int i = 0; i < self.array.count; i++) {
        
        UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width * i, 0, self.frame.size.width, self.contentView.frame.size.height)];
        
//        HeadScrollView *scrollImageView = [[HeadScrollView alloc] init];
        
        scrollImageView.userInteractionEnabled = YES;
        
        UILabel *labelOfTitle = [[UILabel alloc] init];
        
        scrollImageView.tag = i + 100;
        
        Model *model = self.array[i];
        
        [scrollImageView sd_setImageWithURL:[NSURL URLWithString:model.imglink] placeholderImage:[UIImage imageNamed:@"jzz"]];
        
        labelOfTitle.frame = CGRectMake(self.frame.size.width * i + 15, self.frame.size.height - 35 , self.frame.size.width, 30);
        labelOfTitle.textColor = [UIColor whiteColor];
        
        labelOfTitle.text = model.title;
        
        scrollImageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelScroll:)];
        
        [scrollImageView addGestureRecognizer:tap];
        
        [self.scroll addSubview:scrollImageView];
        [self.scroll addSubview:labelOfTitle];
        
        [labelOfTitle release];
    }
    
    
    [self addTimer];
    
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    // 判断偏移量(x),
    if (scrollView.contentOffset.x == self.scroll.frame.size.width * (self.array.count - 1)) {
        
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
    }
    
    if (scrollView.contentOffset.x == 0) {
        
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width * (self.array.count - 2), 0) animated:NO];
    }
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
